package com.exam.dao.ti;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.exam.common.vo.ExamTiVO;
import com.exam.common.vo.TiVO;
import com.exam.common.vo.page.PageModel;
import com.exam.common.vo.page.SystemContext;
import com.exam.dao.base.BaseDaoImpl;
import com.exam.entity.ExamSubject;
import com.exam.entity.ExamTi;
import com.exam.entity.ExamTiDifficulty;
import com.exam.entity.ExamTiType;

@Repository("examTiDaoImp")
public class ExamTiDaoImp extends BaseDaoImpl<ExamTi> implements ExamTiDao{

	@SuppressWarnings("unchecked")
	@Override
	public PageModel<ExamTiVO> findByPage() {
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		
		PageModel<ExamTiVO> page = new PageModel<ExamTiVO>();
		
		List<ExamTiVO> datas = new ArrayList<ExamTiVO>();
		List<ExamTi> tempList = this.getSession().createQuery(" from ExamTi")
				.setFirstResult(offset).setMaxResults(pageSize).list();
		for(ExamTi ti:tempList){
			ExamTiVO tiVO = new ExamTiVO();
			BeanUtils.copyProperties(ti, tiVO);
			datas.add(tiVO);
		}
		Long totalSize = (Long) this.getSession().createQuery("select count(*) from ExamTi").uniqueResult();
		page.setOffset(offset);
		page.setDatas(datas);
		page.setPageSize(pageSize);
		page.setTotalSize(totalSize);
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<Integer, String> getTiDifficulty() {
		List<ExamTiDifficulty> tiList = this.getSession().createQuery("from ExamTiDifficulty").list();
		Map<Integer,String> tiMap = new HashMap<Integer,String>();
		
		if(tiList!=null){
			for(ExamTiDifficulty tiDiff:tiList){
				tiMap.put(tiDiff.getExamTiDifficulty(), tiDiff.getExamTiDifficultyName());
			}
		}
		
		return tiMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<Integer, String> getTiType() {
		List<ExamTiType> tiList = this.getSession().createQuery("from ExamTiType").list();
		Map<Integer,String> tiTypeMap = new HashMap<Integer,String>();
		
		if(tiList!=null){
			for(ExamTiType tiType:tiList){
				tiTypeMap.put(tiType.getExamTiType(), tiType.getExamTiTypeName());
			}
		}
		
		return tiTypeMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<Integer, String> getSubject() {
		List<ExamSubject> tiList = this.getSession().createQuery("from ExamSubject").list();
		Map<Integer,String> subjectMap = new HashMap<Integer,String>();
		
		if(tiList!=null){
			for(ExamSubject subject:tiList){
				subjectMap.put(subject.getExamSubjectId(), subject.getExamSubjectName());
			}
		}
		return subjectMap;
	}

	@Override
	public ExamTiVO loadByTiId(int tiId) {
		ExamTiVO tiVO = new ExamTiVO();
		ExamTi ti = (ExamTi) this.getSession().createQuery(" from ExamTi where examTiId = :tiId ")
				.setParameter("tiId", tiId)
				.uniqueResult();
		if(ti != null){
			BeanUtils.copyProperties(ti, tiVO);
		}else{
			tiVO = null;
		}
		return tiVO;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PageModel<TiVO> findPage() {
		
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		
		PageModel<TiVO> page = new PageModel<TiVO>();
		
		List<TiVO> list = new ArrayList<TiVO>();
		StringBuffer sb = new StringBuffer();
		sb.append("select ti.exam_ti_id as examTiId,ti.exam_ti_name as examTiName,ti.exam_ti_optiona as examTiOptiona,ti.exam_ti_optionb as examTiOptionb,");
		sb.append("ti.exam_ti_optionc as examTiOptionc,ti.exam_ti_optiond as examTiOptiond,ti.exam_ti_key as examTiKey,");
		sb.append("ti.exam_ti_point as examTiPoint,ti.exam_ti_difficulty as examTiDifficulty,ti.exam_ti_type as examTiType,");
		sb.append("sub.exam_subject_name as examSubjectName,type.exam_ti_type_name as examTiTypeName,dif.exam_ti_difficulty_name as examTiDifficultyName");
		sb.append("  from exam_ti as ti left join exam_subject as sub on ti.exam_subject_id = sub.exam_subject_id ");
	    sb.append("left join exam_ti_type as type on ti.exam_ti_type = type.exam_ti_type ");
	    sb.append("left join exam_ti_difficulty as dif on ti.exam_ti_difficulty = dif.exam_ti_difficulty");
	    
	    Query query = this.getSession().createSQLQuery(sb.toString())
	    		.addScalar("examTiId",IntegerType.INSTANCE)
	    		.addScalar("examTiName",StringType.INSTANCE)
	    		.addScalar("examTiOptiona",StringType.INSTANCE)
	    		.addScalar("examTiOptionb",StringType.INSTANCE)
	    		.addScalar("examTiOptionc",StringType.INSTANCE)
	    		.addScalar("examTiOptiond",StringType.INSTANCE)
	    		.addScalar("examTiKey",StringType.INSTANCE)
	    		.addScalar("examTiPoint",IntegerType.INSTANCE)
	    		.addScalar("examTiDifficulty",IntegerType.INSTANCE)
	    		.addScalar("examTiType",IntegerType.INSTANCE)
	    		.addScalar("examSubjectName",StringType.INSTANCE)
	    		.addScalar("examTiTypeName",StringType.INSTANCE)
	    		.addScalar("examTiDifficultyName",StringType.INSTANCE)
	    		.setResultTransformer(Transformers.aliasToBean(TiVO.class));
		
	    Long totalSize = (long) query.list().size();
	    list = query.setFirstResult(offset).setMaxResults(pageSize).list();
	    
	    
	    for(TiVO ti : list){
	    	String tiName = ti.getExamTiId()+"."+ ti.getExamTiName().replace("\n", "<br/>");
	    	String tiOption = "<br/>A."+ti.getExamTiOptiona()+"&nbsp;&nbsp;&nbsp;B."
	    			+ti.getExamTiOptionb()+"&nbsp;&nbsp;&nbsp;C."+ti.getExamTiOptionc()+"&nbsp;&nbsp;&nbsp;D."
	    			+ti.getExamTiOptiond();
	    	ti.setExamTiName(tiName+tiOption);
	    }
	    
		page.setOffset(offset);
		page.setDatas(list);
		page.setPageSize(pageSize);
		page.setTotalSize(totalSize);
		return page;
		
	}
	

	

}
